/**
 *
 * @param {string[]} strs
 * @returns {string}
 */
function longestCommonPrefix(strs) {
    let objectStr = {
        minLength: strs[0].length,
        data: [strs[0]],
        commonPrefix: "",
    };
    strs.forEach((p) => {
        const pLength = p.length;

        if (objectStr.minLength > pLength) {
            objectStr.minLength = pLength;
            objectStr.data = [p];
            return;
        }

        if (objectStr.minLength === pLength) {
            objectStr.data.push(p);
        }
    });

    objectStr.data.forEach((common) => {
        let length = common.length;

        for (let i = length; i > 0; i--) {
            let cutString = common.slice(0, i);
            if (strs.every((p) => p.startsWith(cutString))) {
                objectStr.commonPrefix = cutString;
                return;
            }
        }
    });

    return objectStr.commonPrefix;
}
const strs = ["flower", "flow", "flight"];

console.log(longestCommonPrefix(strs));
